package me.bay.gt.java.config;

import me.bay.gt.java.school.Klass;
import me.bay.gt.java.school.School;
import me.bay.gt.java.school.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;

@Configuration
@ComponentScan("me.bay.gt.java.school")
@EnableConfigurationProperties(SpringBootPropertiesConfiguration.class)
public class W5E8AutoConfiguration {
    @Autowired
    private SpringBootPropertiesConfiguration props;

    @Bean
    public Student student1() {
        Student student = new Student();
        student.setId(1);
        student.setName("bay");
        return student;
    }

    @Bean
    public Student student2() {
        Student student = new Student();
        student.setId(2);
        student.setName("JayZhou");
        return student;
    }

    @Bean
    public Klass class1() {
        Klass class1 = new Klass();
        class1.setStudents(new ArrayList<Student>() {{
            add(student1());
            add(student2());
        }});
        return class1;
    }

    @Bean
    public School school() {
        School school = new School();
        school.setClass1(class1());
        if (props.getSchoolName() != null) {
            school.setName(props.getSchoolName());
        } else {
            school.setName("星战实验学校");
        }
        return school;
    }
}
